package com.boot.config

import org.springframework.context.annotation.Configuration
import org.springframework.scheduling.annotation.EnableScheduling
import org.springframework.scheduling.annotation.Scheduled
import org.springframework.stereotype.Component

import java.io.File
import java.time.LocalDateTime

/**
 * 为了避免服务器空间爆表，每晚定时清理当天生成的二维码图片
 */
@Component
@Configuration
@EnableScheduling
class CleanQRimgTask {
    @Scheduled(cron = "0 0 0 * * ?")
    private fun configureTasks() {
        System.err.println("定时任务-清除当天生成的预约二维码图片: " + LocalDateTime.now())
        delFile("/logs")
    }

    private fun delFile(path: String) {
        val file = File(path)
        if (!file.exists()) {
            return
        }
        if (file.isDirectory) {
            val files = file.listFiles()
            for (f in files!!) {
                if (f.name.startsWith("qrcode"))
                    f.delete()
            }
        }
    }
}
